﻿#region

using System;
using System.Timers;
using EsbMonitorExtension.MonitorTasks.Base;
using EsbMonitorMessages.Events;
using log4net;
using NServiceBus.Unicast;

#endregion

namespace EsbMonitorExtension.MonitorTasks
{
    public class InitServiceAliveServiceTask : BaseMonitorTask
    {
        private static readonly ILog Logger = LogManager.GetLogger(typeof (InitServiceAliveServiceTask));

        public InitServiceAliveServiceTask(IUnicastBus bus)
            : base(bus)
        {
        }

        public override void RunTask()
        {
            Logger.Info("Running InitServiceAliveServiceTask");

            Timer timer = new Timer(500);
            // Set the Interval to 2 minutes (1000 * 60 * 2).
            timer.Interval = 1000*60*2;
            timer.AutoReset = true;
            timer.Elapsed += OnElapsed;
            timer.Start();

            Logger.Info("Finished InitServiceAliveServiceTask");
        }

        private void OnElapsed(object sender, ElapsedEventArgs elapsedEventArgs)
        {
            Logger.Info("Sending ServiceAlive message");

            Bus.Send<IServiceAlive>(m =>
                                        {
                                            m.ServiceInfo = Bus.ServiceInfo();
                                            m.EventTime = DateTime.Now;
                                        });
        }
    }
}